﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace Common.DBUtility
{
    public class CalendarConversion
    {
        /// <summary>
        /// 实例化一个  ChineseLunisolarCalendar  
        /// </summary>
        private static ChineseLunisolarCalendar ChineseCalendar = new ChineseLunisolarCalendar();
        ///<summary> 
        ///十天干 
        ///</summary>
        private static string[] tg = { "甲", "乙", "丙", "丁", "戊", "己", "庚", "辛", "壬", "癸" };
        ///<summary>
        ///十二地支
        ///</summary>
        private static string[] dz = { "子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥" };
        /// <summary>
        ///十二生肖
        ///</summary>           
        private static string[] sx = { "鼠", "牛", "虎", "免", "龙", "蛇", "马", "羊", "猴", "鸡", "狗", "猪" };
        /// <summary>
        /// 返回农历天干地支年        
        /// </summary>
        ///<param name="year">农历年</param> 
        ///<returns></returns>
        public static string GetLunisolarYear(int year)
        {
            if (year > 3)
            {
                int tgIndex = (year - 4) % 10;
                int dzIndex = (year - 4) % 12;
                return string.Concat(tg[tgIndex], dz[dzIndex], "[", sx[dzIndex], "]");
            }
            throw new ArgumentOutOfRangeException("无效的年份!");
        }
        /// <summary>        
        /// 农历月        
        /// </summary>        
        /// <returns></returns>   
        private static string[] months = { "正", "二", "三", "四", "五", "六", "七", "八", "九", "十", "十一", "十二(腊)" };
        /// <summary>  
        /// 农历日     
        /// </summary>  
        private static string[] days1 = { "初", "十", "廿", "三" };
        /// <summary>    
        /// 农历日
        /// </summary>
        private static string[] days = { "一", "二", "三", "四", "五", "六", "七", "八", "九", "十" };
        /// <summary>
        /// 返回农历月
        /// </summary>
        /// <param name="month">月份</param>
        /// <returns></returns>
        public static string GetLunisolarMonth(int month)
        {
            if (month < 13 && month > 0)
            {
                return months[month - 1];
            }
            throw new ArgumentOutOfRangeException("无效的月份!");
        }
        /// <summary>
        /// 返回农历日
        /// </summary>
        /// <param name="day">天</param>
        /// <returns></returns>
        public static string GetLunisolarDay(int day)
        {
            if (day > 0 && day < 32)
            {
                if (day != 20 && day != 30)
                {
                    return string.Concat(days1[(day - 1) / 10], days[(day - 1) % 10]);
                }
                else
                {
                    return string.Concat(days[(day - 1) / 10], days1[1]);
                }
            }
            throw new ArgumentOutOfRangeException("无效的日!");
        }

        #region GetChineseDateTime(DateTime datetime) 根据公历日期获取农历日期
        /// <summary>
        /// 根据公历获取农历日期
        /// </summary>
        /// <param name="datetime">公历日期</param>
        /// <returns></returns>
        public static string GetChineseDateTime(DateTime datetime)
        {
            int year = ChineseCalendar.GetYear(datetime);
            int month = ChineseCalendar.GetMonth(datetime);
            int day = ChineseCalendar.GetDayOfMonth(datetime);
            //获取闰月， 0 则表示没有闰月
            int leapMonth = ChineseCalendar.GetLeapMonth(year);
            bool isleap = false;
            if (leapMonth > 0)
            {
                if (leapMonth == month)
                {
                    //闰月
                    isleap = true;
                    month--;
                }
                else if (month > leapMonth)
                {
                    month--;
                }
            }
            return string.Concat(GetLunisolarYear(year), "年", isleap ? "闰" : string.Empty, GetLunisolarMonth(month), "月", GetLunisolarDay(day));
        }
        #endregion

        #region GetDateFromLunarDate(string datetime, bool IsLeapMonth)根据农历获取公历日期
        /// <summary>
        /// 根据农历获取公历日期
        /// </summary>
        /// <param name="datetime">日期字符串</param>
        /// <param name="IsLeapMonth">是否闰月</param>
        /// <returns></returns>
        public static DateTime GetDateFromLunarDate(string datetime, bool IsLeapMonth)
        {
            DateTime dt;
            DateTime dateOut=DateTime.Now;
            DateTime.TryParse(datetime,out dt);
            dateOut = ChineseCalendar.ToDateTime(dt.Year, dt.Month, dt.Day, dt.Hour, dt.Minute, dt.Second, dt.Millisecond);
            if (IsLeapMonth)
            {
                dateOut = dateOut.AddDays(GetDaysFromMonth(dt.Year,dt.Month));
            }
            return dateOut;
        }
        #endregion

        #region GetLeapMonth(int year)获取闰月
        /// <summary>
        /// 获取闰月
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        public static int GetLeapMonth(int year)
        {
            return ChineseCalendar.GetLeapMonth(year)-1;
        }
        #endregion

        #region GetDayFromLeapMonth(int year) 获取闰月的天数
        /// <summary>
        /// 获取闰月的天数
        /// </summary>
        /// <param name="year"></param>
        /// <returns></returns>
        public int GetDayFromLeapMonth(int year)
        {
            return ChineseCalendar.GetDaysInMonth(year, ChineseCalendar.GetLeapMonth(year));
        }
        #endregion

        #region GetDaysFromMonth(int year,int month)获取该月天数
        /// <summary>
        /// 获取该月天数
        /// </summary>
        /// <param name="year">year</param>
        /// <param name="month">month</param>
        /// <returns></returns>
        public static int GetDaysFromMonth(int year,int month)
        {
            return ChineseCalendar.GetDaysInMonth(year, month);
        }
        #endregion

        #region  GetDaysInMonth(int year,int month) 获取月份天数
        /// <summary>
        /// GetDaysInMonth(int year,int month) 获取月份天数
        /// </summary>
        /// <param name="year">年</param>
        /// <param name="month">月</param>
        /// <returns></returns>
        public int GetDaysInMonth(int year,int month)
        {
            return ChineseCalendar.GetDaysInMonth(year, month);
        }
        #endregion

        #region GetXXX(DateTime dt) 根据公历返回农历年月日
        /// <summary>
        /// 根据公历返回农历年
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static int GetYear(DateTime dt)
        {
            return ChineseCalendar.GetYear(dt);
        }

        /// <summary>
        /// 根据公历返回农历月
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static int GetMonth(DateTime dt)
        {
            return ChineseCalendar.GetMonth(dt);
        }
        /// <summary>
        /// 根据公历返农历回天
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static int GetDay(DateTime dt)
        {
            return ChineseCalendar.GetDayOfMonth(dt);
        }
        #endregion

        #region GetIsLeap ()判断当前月是否为润月
        /// <summary>
        /// 判断当前月是否为润月
        /// </summary>
        /// <returns></returns>
        public static bool GetIsLeap()
        {
            return ChineseCalendar.GetMonth(DateTime.Now) == ChineseCalendar.GetLeapMonth(DateTime.Now.Year);
        }
        #endregion
    }
}
